<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<link rel="stylesheet" type="text/css" media="screen" href="css/smoothness/jquery-ui-1.9.0.custom.css" />	<!-- jqueryUI CSS -->
<link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />							<!-- jqGrid CSS -->
<script src="js/jquery-1.8.0.min.js" type="text/javascript"></script>										<!-- jQuery 核心 -->
<script src="js/grid.locale-tw.js" type="text/javascript"></script>											<!-- jqGrid 語系檔 -->
<script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>										<!-- jqGrid 核心 -->
<script src="js/jquery-ui-1.9.0.custom.min.js" type="text/javascript"></script>							<!-- jqueryUI 核心 -->


<title>維護環境</title>

<script type="text/javascript">

function checkDuplicated() {
	var envId = $("#envid")[0].value;
	var ll = $("#grid1").jqGrid('getDataIDs');
	var isDuplicated = false;
	for(var i=0;i<ll.length;i++){
		if (ll[i] == envId ) {
			isDuplicated = true;
		}
	}
	
	if (isDuplicated) {
		alert(envId+"已經存在，環境ID不可重覆 !");
	} else {
		var envName = $("#envname")[0].value;
		var subSysId = $("#subsysid")[0].value;
		var subSysName = $("option[value='"+subSysId+"']").text();
		var dataAdd = {"envid":envId,"envname":envName,"subsysid":subSysName,"orisubsysid":subSysId};
		var _iCount = $("#grid1").jqGrid('getGridParam', 'records');
		$("#grid1").jqGrid('addRowData', _iCount+1,dataAdd);
	} 
	return [];
}

function init (strTmp) {
	$.post("QueryEnv",$("#q_form").serializeArray(),function(res) {
		$.each(res,function(i, tmp) {
			strTmp += tmp.subSysId+":'"+tmp.subSysName+"',";
		});
		strTmp = strTmp.substring(0, strTmp.length-1) + "}";
		$("#subsyses")[0].value = strTmp;
	},"JSON");
} 

		

$(document).ready(function() {
	var st = "{";
	$("#getDataType")[0].value = "JSON";
	init(st);
	subSysList = $("#subsyses")[0].value;
	$("#getDataType")[0].value = "";
	
	$("#grid1").jqGrid({
		url:'QueryEnv',
		datatype: "json",
		mtype: "post",
	   	colNames:['環境ID', '環境名稱','子系統',''],
	   	colModel:[
	   		{name:'envid',index:'envid',key:true,editable:true,editrules:{required:true}},
	   		{name:'envname',index:'envname',editable:true,editrules:{required:true}},
	   		{name:'subsysid',editable:true, edittype:'select', editoptions: { multiple: false, value:eval('(' + subSysList + ')') }},
	   		{name:'orisubsysid',hidden:true}
	   	],
	   	width:460,
	   	rowNum:10,
	   	loadonce:true,
	   	rowList:[10,20,30],
	   	pager: '#pager',
	   	rownumbers: true,
	   	viewrecords: true,	
		editurl: "ManipulateGridEnv",
	    caption:"設定環境"
	});
	
	jQuery("#grid1").navGrid('#pager',{edit:true,add:true,del:true}
	,{	/**
		Edit
			按下送出後要完成下列功能
			1. 畫面更新為新輸入的資料
			2. 後端更改新值成功
		**/
		editData:{ orisubsysid: function() {
			var sel_id = $("#grid1").jqGrid('getGridParam', 'selrow');
			return $("#grid1").jqGrid('getCell', sel_id, 'orisubsysid');
		}},
		afterSubmit:function() {
			var envId = $("#envid")[0].value;
			var envName = $("#envname")[0].value;
			var subSysId = $("#subsysid")[0].value;
			var subSysName = $("option[value='"+subSysId+"']").text();
			
			var dataAdd = {"envid":envId,"envname":envName,"subsysid":subSysName,"orisubsysid":subSysId};
			$("#grid1").jqGrid('setRowData',envId,dataAdd);
			return [];
		}
	}
	,{	// Add
		onclickSubmit:function() {
			checkDuplicated();
		}
	}
	,{	// Delete
		delData:{ orisubsysid: function() {
			var sel_id = $("#grid1").jqGrid('getGridParam', 'selrow');
			return $("#grid1").jqGrid('getCell', sel_id, 'orisubsysid');
		}}  
	}
);
	
});
</script>

</head>
<body>
<table id="grid1"></table>
<div id="pager"></div>
<form id="q_form">
	<input type="hidden" id="getDataType" name="getDataType" />
</form>
<input id="subsyses" type="hidden" >
</body>
</html>